package com.video.controller;

import com.common.domain.dto.DraftVideoAddDTO;
import com.common.domain.dto.DraftVideoQueryByConditionDTO;
import com.common.domain.dto.DraftVideoUpdateDTO;
import com.common.domain.po.DraftVideo;
import com.common.domain.vo.DraftVideoQueryByIdVO;
import com.common.domain.vo.DraftVideoQueryByPageVO;
import com.common.domain.vo.DraftVideoQueryVideoAndItemById;
import com.common.domain.vo.VideoExcelExport;
import com.common.result.Result;
import com.common.utils.ExportUtil;
import com.video.service.DraftVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 路亚
 * @version 0.1
 * @className DraftVideoController
 * @description
 * @date 2024/12/10 22:44
 * @since jdk11
 */
@RestController
@RequestMapping("/draft/video")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "草稿短剧管理")
public class DraftVideoController {

    private final DraftVideoService draftVideoService;

    private final ExportUtil exportUtil;

    @PostMapping("/add")
    @ApiOperation("添加短剧和短剧行")
    public Result add(@RequestBody DraftVideoAddDTO draftVideoAddDTO){
        log.info("添加短剧,添加数据为:{}", draftVideoAddDTO);
        draftVideoService.add(draftVideoAddDTO);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改草稿短剧")
    public Result update(@RequestBody DraftVideoUpdateDTO draftVideoAddDTO){
        log.info("修改草稿,修改数据为:{}", draftVideoAddDTO);
        draftVideoService.updateByDraftId(draftVideoAddDTO);
        return Result.success();
    }

    @PostMapping("/postVideo")
    @ApiOperation("上架短剧")
    public Result postVideo(Long id){
        log.info("上架短剧,id为:{}", id);
        draftVideoService.postVideo(id);
        return Result.success();
    }

    @PostMapping("/downVideo")
    @ApiOperation("下架短剧")
    public Result downVideo(Long id){
        log.info("下架短剧,id为:{}", id);
        draftVideoService.downVideo(id);
        return Result.success();
    }

    @GetMapping("/getExcel")
    @ApiOperation("导出短剧Excel表")
    public Result getExcel(){
        List<DraftVideo> draftVideoList = draftVideoService.list();
        List<VideoExcelExport> list = new ArrayList<>();
        for (DraftVideo draftVideo : draftVideoList) {
            VideoExcelExport videoExcelExport = new VideoExcelExport();
            BeanUtils.copyProperties(draftVideo, videoExcelExport);
            list.add(videoExcelExport);
        }
        try {
            exportUtil.expExcel("短剧管理", list, VideoExcelExport.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除短剧")
    public Result delete(Long id){
        log.info("删除短剧,id为:{}", id);
        draftVideoService.removeDraftById(id);
        return Result.success();
    }

    @GetMapping("/queryByCondition")
    @ApiOperation("根据条件分页查询草稿视频")
    public Result queryByCondition(DraftVideoQueryByConditionDTO dto, Integer page, Integer pageSize){
        log.info("根据条件分页查询草稿视频,dto:{}", dto);
        DraftVideoQueryByPageVO result = draftVideoService.queryByCondition(dto, page, pageSize);
        return new Result(200, "OK", result);
    }

    @GetMapping("/queryById")
    @ApiOperation("根据id查询草稿视频详情")
    public Result queryById(Long id){
        log.info("根据id查询草稿视频详情,id:{}", id);
        DraftVideoQueryByIdVO result = draftVideoService.queryById(id);
        return new Result(200, "OK", result);
    }

    @GetMapping("/queryVideoAndItemById")
    @ApiOperation("根据id查询视频和视频行详情")
    public Result queryVideoAndItemById(Long id, Integer num){
        DraftVideoQueryVideoAndItemById result = draftVideoService.queryVideoAndItemById(id, num);
        return new Result(200, "OK", result);
    }
}
